package com.test.ip;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.MemorySize;
import org.apache.flink.configuration.TaskManagerOptions;
import org.apache.flink.table.api.EnvironmentSettings;
import org.apache.flink.table.api.TableEnvironment;
import org.apache.flink.table.api.TableResult;
import org.apache.flink.table.catalog.hive.HiveCatalog;

/**
 * @create: 2023-07-13 23:42
 * --------------
 * @notes:
 **/
public class Q4 {
    public static void main(String[] args) {
        EnvironmentSettings settings = EnvironmentSettings
                .newInstance()
                .inBatchMode()
                .build();
        TableEnvironment tableEnv = TableEnvironment.create(settings);


        // 创建hive的catalog
        String catalogName = "myhive";
        HiveCatalog hiveCatalog = new HiveCatalog(
                catalogName,
                "default",
                "src/main/resources",
                "3.1.2"
        );
        //注册catalog
        tableEnv.registerCatalog(catalogName,hiveCatalog);
        tableEnv.useCatalog(catalogName);
        getDtDistinctCnt(tableEnv);
    }

    private static void getDtDistinctCnt(TableEnvironment tableEnv) {
        TableResult result = tableEnv.executeSql(
                "SELECT dt,province,count(distinct account_id) as cnt_login \n" +
                        "FROM test.login_data_province_dt \n" +
                        "GROUP BY dt, province");
        result.print();
    }
}
